// pages/apply-return/index.js
import { ordeRefundReason, orderRefundVerifyComplaint, getOrderDetail } from '../../api/order.js';
import util from '../../utils/util.js';
import { sendDingyue } from '../../api/api.js';
import { openOrderRefundSubscribe, openOrderRefundSuccessSubscribe } from '../../utils/SubscribeMessage.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '投诉',
      'color': false
    },
    refund_reason_wap_img: [],
    orderInfo: {},
    RefundArray: [],
    index: 0,
    orderId: 0,
  },
  /**
   * 授权回调
   * 
  */
  onLoadFun: function () {
    this.getOrderInfo();
    this.getRefundReason();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!options.orderId) return app.Tips({ title: '缺少订单id,无法退款' }, { tab: 3, url: 1 });
    this.setData({ orderId: options.orderId });
  },
  /**
   * 获取订单详情
   * 
  */
  getOrderInfo: function () {
    var that = this;
    getOrderDetail(that.data.orderId).then(res => {
      that.setData({ orderInfo: res.data });
    });
  },
  /**
   * 获取退款理由
  */
  getRefundReason: function () {
    var that = this;
    ordeRefundReason().then(res => {
      that.setData({ RefundArray: res.data });
    })
  },

  /**
   * 删除图片
   * 
  */
  DelPic: function (e) {
    var index = e.target.dataset.index, that = this, pic = this.data.refund_reason_wap_img[index];
    that.data.refund_reason_wap_img.splice(index, 1);
    that.setData({ refund_reason_wap_img: that.data.refund_reason_wap_img });
  },

  /**
   * 上传文件
   * 
  */
  uploadpic: function () {
    var that = this;
    util.uploadImageOne('upload/image', function (res) {
      that.data.refund_reason_wap_img.push(res.data.url);
      that.setData({ refund_reason_wap_img: that.data.refund_reason_wap_img });
    });
  },


  /**
   * 申请退货
  */
  subRefund: function (e) {
    var that = this, value = e.detail.value;
    var orderInfo = that.data.orderInfo;
    //收集form表单
    // if (!value.refund_reason_wap_explain) return app.Tips({title:'请输入退款原因'});
    openOrderRefundSubscribe().then(temp => {
      /**取消订单通知 */
      var temp_keys = Object.keys(temp)[0];
      var send_data = {
        order_no: orderInfo.order_id,
        price: orderInfo.pay_price,
        product_name: orderInfo['cartInfo'][0]['productInfo']['store_name'],
        toUrl: "/pages/order_details/index?order_id=" + orderInfo.order_id,
        tempid: temp_keys
      }
      orderRefundVerifyComplaint({
        text: that.data.RefundArray[that.data.index] || '',
        complaint_reason_wap_explain: value.refund_reason_wap_explain,
        complaint_reason_wap_img: that.data.refund_reason_wap_img.join(','),
        uni: that.data.orderId
      }).then(res => {
        // return app.Tips({ title: '申请成功', icon: 'success' }, { tab: 5, url: '/pages/user_return_list/index?isT=1' });

        return app.Tips({ title: '申请成功', icon: 'success' }, { tab: 5, url: '/pages/order_list/index' });

      }).catch(err => {
        return app.Tips({ title: err });
      })

    });
  },

  bindPickerChange: function (e) {
    this.setData({ index: e.detail.value });
  },

})